package com.relatev.minecraft.TRMobAPI;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

public class CommandManager {

    public static boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        sender.sendMessage("§3------§a§lTRMobAPI§3------");
        if (args.length == 0) {
            sender.sendMessage("§nhttps://www.relatev.com/forum.php?mod=viewthread&tid=2409");
            sender.sendMessage("§d作者缇亚祢|QQ1207223090");
            sender.sendMessage("§c/" + label + " help 以查阅帮助");
            return true;
        }
        if (args[0].equalsIgnoreCase("help")) {
            sender.sendMessage("§espawn [ID] [Level] 生成一只生物");
            sender.sendMessage("§ekillall [ID] 清理所有指定类型的生物");
            sender.sendMessage("§elist 列出怪物列表");
        }
        if (sender.hasPermission("TRMobAPI.admin")) {
            if (args[0].equalsIgnoreCase("updateon")) {
                TRMobAPI.getAPI().getTRConfigManager().CoreConfig.set("AutoUpdate", true);
                try {
                    TRMobAPI.getAPI().getTRConfigManager().CoreConfig.save(TRMobAPI.getAPI().getTRConfigManager().CoreConfigFile);
                } catch (IOException ex) {
                    Logger.getLogger(CommandManager.class.getName()).log(Level.SEVERE, null, ex);
                }
                sender.sendMessage("§a自动更新已打开！");
            }
            if (args[0].equalsIgnoreCase("spawn") && sender instanceof Player) {
                if (args.length == 1) {
                    sender.sendMessage("§c请输入怪物名字");
                    return true;
                }
                TRMob trm = TRMobAPI.getAPI().getTRMobManager().fuzzyRecognizeTRMob(args[1]);
                if (trm == null) {
                    sender.sendMessage("§c错误，找不到这种怪物");
                    return true;
                }
                if (args.length == 2) {
                    sender.sendMessage("§c请输入怪物等级");
                    return true;
                }
                trm.spawn(((Player) sender).getLocation(), Integer.parseInt(args[2]));
                sender.sendMessage("§a" + trm.ID + ",等级为" + Integer.parseInt(args[2]) + " 已生成");
            }
            if (args[0].equalsIgnoreCase("killall")) {
                if (args.length == 1) {
                    sender.sendMessage("§c请输入怪物名字");
                    return true;
                }
                TRMob trm = TRMobAPI.getAPI().getTRMobManager().fuzzyRecognizeTRMob(args[1]);
                if (trm == null) {
                    sender.sendMessage("§c错误，找不到这种怪物");
                    return true;
                }
                int count = 0;
                for (Entity ent : trm.getLivingMobs()) {
                    ent.remove();
                    count++;
                }
                sender.sendMessage("§e清理了" + count + "只" + trm.ID);
            }
            if (args[0].equalsIgnoreCase("list")) {
                sender.sendMessage("" + TRMob.getTRMobs());
            }
        }
        return true;
    }
}
